#ifndef __GRABCUT_FILTER_H__
#define __GRABCUT_FILTER_H__

#include <highgui.h>
#include <cv.h>
#include <iostream>

#include <Filter.h>
#include <Picture.h>

/* GrabcutFilter class
 * inherits the interface of Filter so it has a functionality
 * of displaying image to the screen and interract with user;
 * the main and only responsibility of this class is to exectue the
 * grabCut algorithm on a given picture.
 * The grabCut can be called several times, each time with a modified
 * mask of background and foreground parts of the image.
 */
class GrabcutFilter : public Filter {
    public:
        enum State {
            NOT_SET = 0,
            IN_PROCESS = 1,
            SET = 2
        };

        /* C'tor gets a picture and an ROI.
         * the ROI can be retrived multiple ways, for it's simply
         * a vector of Rects.
         * The mask of grabCut will be initialized according to this ROI;
         */
        GrabcutFilter(const Picture& pic, vector<cv::Rect>& ROI);
        ~GrabcutFilter();

        /* Show the image in a gray scale version,
         * except the ROI, which is preserved with original colors; */
        virtual bool refresh(cv::Mat& image) const;
        /* Exectue a grabCut iteration. moreData is a pointer to 
         * the mask of the grabCut (background ade foreground mask) */
        virtual int nextIter(void* moreData=0);
        virtual boost::shared_ptr<Picture> exportPicture();
        vector< vector < cv::Mat > >exportROIs();
    private:
        /* If someone changes the picture that grabcut uses,
         * then grabcut will not be valid anymore */
        const Picture& picture;
        vector<cv::Rect> ROI;
        cv::Mat mask;
        bool isValidated;
        int iterCount;
        State state;

        /* inner opencv grabcut-state; we must provide them,
         * but must not touch them; */
        cv::Mat bgdModel, fgdModel;

        /* no default copy c'tors */
        GrabcutFilter(const GrabcutFilter& p); // no default copy c'tor
        GrabcutFilter& operator=(const GrabcutFilter& p); // no default copy c'tor
        
        /* initializer */
        bool initialized;        
        bool init();
        void destroy();
        /* init mask with a background value in all of the picure
         * execept the ROIs which are initialized as foreground */
        void initMask();
};


#endif // __GRABCUT_FILTER_H__
